﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace N03_11HCA_HTTTHD.Controllers
{
    public class RaoVatController : Controller
    {
        Data_DAO_DTO_Lib.RAOVAT_DAO raoVatDao = new Data_DAO_DTO_Lib.RAOVAT_DAO();
        Data_DAO_DTO_Lib.RAOVATDUOCQUANTAM_DAO raoVatQuanTamDao = new Data_DAO_DTO_Lib.RAOVATDUOCQUANTAM_DAO();

        //
        // GET: /RaoVat/
        public ActionResult Index()
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> list = raoVatDao.LayDanhSachCoThamSo(0, TienIch.HeThong.SoLuongLoadRaoVatMacDinh, TienIch.HeThong.ChuoiSapXepRaoVatMacDinh);
            Models.RaoVat modelRaoVat = new Models.RaoVat(list);

            return View("RaoVat", modelRaoVat);
        }

        [HttpPost]
        public JsonResult QuanTam(int id)
        {

            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);

            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng quan tâm rao vặt" });    
            }

            try
            {
                Data_DAO_DTO_Lib.DTO.RAOVATDUOCQUANTAM_DTO rvQT = new Data_DAO_DTO_Lib.DTO.RAOVATDUOCQUANTAM_DTO();
                rvQT.IDTaiKhoanQuanTam = (int)N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1;
                rvQT.IDRaoVat = id;
                raoVatQuanTamDao.Them(rvQT);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });    
            }

            return Json(new { Result = "OK"});    
            
        }

        [HttpPost]
        public JsonResult Index(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            List<Data_DAO_DTO_Lib.DTO.DTO> list = raoVatDao.LayDanhSachCoThamSo(jtStartIndex, jtPageSize, jtSorting);

            return new JsonResult() { Data = list };
        }

        public ActionResult ChiTiet(int id_TimMua)
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Data_DAO_DTO_Lib.DTO.RAOVAT_DTO raoVat = (Data_DAO_DTO_Lib.DTO.RAOVAT_DTO)raoVatDao.LayTuID(id_TimMua);
            
            Models.RaoVat_ChiTiet modelTimMua = new Models.RaoVat_ChiTiet(raoVat);

            return View("RaoVat_ChiTiet", modelTimMua);
        }
    }
}
